﻿using Ledict.ConditionExtract.Base;
using Ledict.ConditionExtract.BLL;
using Ledict.ConditionExtract.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Frm.NodeTree
{
    public partial class GridViewCondition : BaseForm
    {
        private readonly TreeDataBLL trbll = new TreeDataBLL();
        private readonly QuoteLawRelBLL quoteLawRelBLL = new QuoteLawRelBLL();
        public string ccName { get; set; }
        public string  ColumnName { get; set; }
        /// <summary>
        /// 查询条件
        /// </summary>
        public string conditionData { get; set; }

        public GridViewCondition()
        {
            InitializeComponent();
        }

        public GridViewCondition(string str)
        {
            InitializeComponent();
            ColumnName = str;
        }

        private void NodeTreeCaseCondition_Load(object sender, EventArgs e)
        {
            this.panel2.Controls.Add(this.comboBox1);
            this.comboBox1.Location= new System.Drawing.Point(18, 23);
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox1.FormattingEnabled = true;



            DataTable dt = new DataTable();
            dt.Columns.Add("id");







            checkedListBox1.DataSource = quoteLawRelBLL.GetHeadWindows();
            checkedListBox1.DisplayMember = "HWName";
            checkedListBox1.ValueMember = "HWType";
        }

        private void t_Case_HeadWords_AfterSelect(object sender, TreeViewEventArgs e)
        {
            var pid = "";
            if (e.Node.Parent != null)
                pid = e.Node.Parent.Tag.ToString();
            Int32 NodeID = Convert.ToInt32(e.Node.Tag.ToString());
            //QuoteLawdt = quoteLawRelBLL.QuoteLawRelAboutCaseList(e.Node.Tag.ToString(), e.Node.Text, e.Node.Name, pid, 0);
            //ProvText.Text = "";

            //DV_QuoteLawReldataPager1.ChangePageNumber(1);
        }

        private void t_Case_HeadWords_AfterExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.Nodes.Clear();
            GetTreeHeadWordsChild(e.Node, "t_Case_HeadWords");
        }

        public void GetTreeHeadWordsChild(TreeNode parentNode, string Tname)
        {
            var dt = trbll.GetTreeHeadWords(parentNode.Tag.ToString(), Tname); // 第一层
            var arrayDR = dt.Select();

            foreach (var CaseTreeNodeRode in arrayDR)
            {
                var node = new TreeNode();

                node.Text = LdFormat.ObjectToString(CaseTreeNodeRode["name"]) + "(" + CaseTreeNodeRode["id"] + ")";
                node.Tag = CaseTreeNodeRode["id"];
                if (trbll.GetTreeHeadWords(node.Tag.ToString(), Tname).Rows.Count > 0) //第二层
                {
                    node.Nodes.Add(new TreeNode());
                }
                parentNode.Nodes.Add(node);
            }
        }

        /// <summary>
        /// 确定按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEnter_Click(object sender, EventArgs e)
        {



            var ChooseWhere = new StringBuilder();
            ChooseWhere.Append(" 1=1 ");
            var ChooseHWType = new StringBuilder();
            var ChooseProv = new StringBuilder();
            var isallHWType = 0;

            #region 类别

            var collection = checkedListBox1.CheckedItems;

            for (var i = 0; i < collection.Count; i++)
            {
                var dr = (DataRowView)collection[i];
                if (string.IsNullOrEmpty(dr.Row["HWType"].ToString())) //包含全部
                {
                    isallHWType = 1;
                    break;
                }
                if (ChooseHWType.ToString() != "")
                {
                    ChooseHWType.Append(",");
                }
                ChooseHWType.Append("'"+dr.Row["HWType"]+"'");
            }
            if (collection.Count > 0 && isallHWType == 0)
                ChooseWhere.Append(" and b.HWType in (" + ChooseHWType + ")");

            #endregion

            if (!string.IsNullOrEmpty(LawKeyWord.Text.Trim()))
                ChooseWhere.Append(" and CATName like '%" + LawKeyWord.Text.Trim() + "%'"); //法律名称

            ChooseWhere.Append(" and   PromuDate >='" + Convert.ToDateTime(PromuDateStart.Text).ToString("yyyy-MM-dd") +
                               "' and PromuDate <='" + Convert.ToDateTime(PromuDateEnd.Text).ToString("yyyy-MM-dd") +
                               "'"); //公布时间
            if (!string.IsNullOrEmpty(ProvKeyWord.Text.Trim()))
            {
                ChooseWhere.Append(" and ProvText like'%" + ProvKeyWord.Text.Trim() + "%'");
            }

            //ChooseWhere.Append(" and (  ");
            //ChooseWhere.Append(ChooseProv);
            //ChooseWhere.Append(" )");
            conditionData = ChooseWhere.ToString();
            this.DialogResult = DialogResult.Yes;
            this.Close();
        }
    }
}
